package com.skt.common_hr.common.memberselector
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class MemberSelectorMediator extends CommonMediator
	{
		public static const NAME:String = "MemberSelectorMediator"; 
		
		public static const SELECTED_MEMBER:String 	= "selectedMember";
		public static const CANCEL_SELECTION:String	= "cancelSelection";
		
		public var selectedMember:MemberVO;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
		public function MemberSelectorMediator()
		{
            super( NAME, null );
		}
		
		private function registerProxies():void
		{
			var proxy:MemberSelectorProxy = facade.retrieveProxy(MemberSelectorProxy.NAME) as MemberSelectorProxy;
			if(proxy == null) {
				proxy = new MemberSelectorProxy();
				facade.registerProxy(proxy);
			}
		}
		
		//silver 0323 
		// 소속부서와 자기부서만 선택할수 있게 바꿀경우는 param이 추가 되어 날라온다.
		public var sO_MGRORG:String="";
		public function exec(parent:DisplayObject, O_MGRORG:String=""):void
		{
			sO_MGRORG = O_MGRORG; //내가 관리할수 있는 그룹만 선택하게하고싶을때 param넘겨받음.

//			var d:Object = Variables.moduleParams;
//			var s:String = Variables.dataSource;
//			var s1:String = Variables.CURRENT_DATA_SOURCE;
			
            registerProxies();
            loadOrgList();
			//loadMemberList(Variables.myOrgCode);
			if(view==null) {
				createPopUp(parent);
			}	
			else {
				PopUpManager.addPopUp(view, parent.root,true);
			}		
			view.setFocus();
			bActivated = true;
			setEventListener();
		}  
		
		private function loadOrgList():void
		{
			var sCheck:String="";
			trace("Variables.moduleName : " + Variables.moduleName);
			if(Variables.moduleName != null && Variables.moduleName == "BIZTRIP") {
				sCheck = "";
			} else {
				sCheck = "X";
			}
			var proxy:MemberSelectorProxy = facade.retrieveProxy(MemberSelectorProxy.NAME) as MemberSelectorProxy;
			proxy.loadOrg(Variables.myPernr, sCheck);
		}
		
		private function loadMemberList(orgCode:String):void
		{
			var proxy:MemberSelectorProxy = facade.retrieveProxy(MemberSelectorProxy.NAME) as MemberSelectorProxy;
			proxy.loadMember(orgCode);
		}
		
		private function setEventListener():void
		{
			view.addEventListener(MemberSelectorEvent.SELECTED_MEMBER, onSelectedMember);
			view.addEventListener(MemberSelectorEvent.SELECTED_ORG, onSelectedOrg);
			view.addEventListener(MemberSelector.CANCEL_SELECTION, onCancelSelection);
			view.addEventListener(SearchNameEvent.SEARCH_NAME, onSearchName);
		}
		
		private function removeEventListener():void
		{
			view.removeEventListener(MemberSelectorEvent.SELECTED_MEMBER, onSelectedMember);
			view.removeEventListener(MemberSelector.CANCEL_SELECTION, onCancelSelection);
		}		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return[
				MemberSelectorProxy.LOAD_MEMBERLIST_COMPLETE,
				MemberSelectorProxy.LOAD_MEMBERLIST_FAILED,
				MemberSelectorProxy.LOAD_ORGLIST_COMPLETE,
				MemberSelectorProxy.LOAD_ORGLIST_FAILED,
				MemberSelectorProxy.LOAD_SEARCH_COMPLETE,
				MemberSelectorProxy.LOAD_SEARCH_FAILED
				];
		}
		
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case MemberSelectorProxy.LOAD_MEMBERLIST_COMPLETE:
					view.memberList = MemberSelectorProxy(note.getBody()).memberList;
					break;
				case MemberSelectorProxy.LOAD_ORGLIST_COMPLETE:
					view.orgList = MemberSelectorProxy(note.getBody()).orgList;
					loadMemberList(Variables.myOrgCode);		//접속자의 소속분원 조회
					view.callLater(view.userDeptfind,[Variables.myOrgCode]);	//접속자의 소속분원  트리 열기
					//view.memberFilter();						//섬세한검색 필터 실행
					view.MemberSort();							//섬세한검색 소팅 실행
					break;
				case MemberSelectorProxy.LOAD_SEARCH_COMPLETE:
					view.memberList = MemberSelectorProxy(note.getBody()).memberList;
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		

		private function createPopUp(parent:DisplayObject):void
		{
			viewComponent = PopUpManager.createPopUp(parent.root, MemberSelector, true);
			PopUpManager.centerPopUp(view);
			view.setFocus();
		}
		
		private function closeModule():void
		{
			removeEventListener();
			view.closeView();
			bActivated = false;
		}
		

		private function onCancelSelection(e:Event):void
		{
			selectedMember = null;
			facade.sendNotification(CANCEL_SELECTION, this);
			closeModule();
		}
		
		
		private function onSelectedMember(e:MemberSelectorEvent):void
		{
			selectedMember = e.member;
			facade.sendNotification(SELECTED_MEMBER, this);
			closeModule();
		}

		private function onSelectedOrg(e:MemberSelectorEvent):void
		{	
			//silver 0323 멤버조회 트리 클릭시 본인부서와 관리부서 이외에는 조회되지 않도록함..
			// 공통모듈이라 조심해야함.
			// sO_MGRORG에 값이 있을경우만 체크하면됨..
			if(sO_MGRORG !="") {
				trace('Variables.myOrgCode : ' + Variables.myOrgCode);
				
				//0512 top일경우 .. 김양숙(2102167) 전체 권한 조회 가능
				//if(Variables.company == "STC" && Variables.myPernr != "2102167" ) {
				/* 				
				//0701
				if(Variables.company == "STC") {
					if(e.orgCode != sO_MGRORG && e.orgCode != Variables.myOrgCode) {
						AlertHelper.showMessage("관리권한이 있는 부서만 조회 하실 수 있습니다.");					
						return;
					}					
				} */
			}
						
			var proxy:MemberSelectorProxy = facade.retrieveProxy(MemberSelectorProxy.NAME) as MemberSelectorProxy;
			proxy.loadMember(e.orgCode);
		}
		  
		private function onSearchName(e:SearchNameEvent):void
		{
			var proxy:MemberSelectorProxy = facade.retrieveProxy(MemberSelectorProxy.NAME) as MemberSelectorProxy;
			proxy.searchName(proxy.orgRoot.@objid, e.name);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():MemberSelector
        {
            return viewComponent as MemberSelector;
        }
	}
}